package com.zito.health.controller;

import com.zito.health.common.result.PlatformResult;
import com.zito.health.entity.ItemCapitalSource;
import com.zito.health.service.ItemCapitalSourceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

/**
 * 项目结题资金进
 */
@Slf4j
@RestController
@RequestMapping("/item/capitalSource")
public class ItemCapitalSourceController {

    @Autowired
    private ItemCapitalSourceService itemCapitalSourceService;

    @PostMapping("/save")
    public PlatformResult save(ItemCapitalSource item){

        if(item.getId() != null && item.getId() > 0){
            itemCapitalSourceService.update(item);
        }else{
            itemCapitalSourceService.save(item);
        }
        return PlatformResult.success();
    }

    @PostMapping("/update")
    public PlatformResult update(ItemCapitalSource item){
        itemCapitalSourceService.update(item);
        return PlatformResult.success();
    }

    @PostMapping("/query")
    public PlatformResult query(ItemCapitalSource item){
        List<ItemCapitalSource> list = itemCapitalSourceService.query(item);
        ItemCapitalSource sum = new ItemCapitalSource();//求和用
        sum.setSubject("经费来源合计");
        sum.setBudget(0.00D);
        sum.setActual(0.00D);
        for(ItemCapitalSource itemCapitalExpend :list){
            BigDecimal b1 = new BigDecimal(Double.toString(sum.getBudget()));
            BigDecimal b2 = new BigDecimal(Double.toString(itemCapitalExpend.getBudget()));
            sum.setBudget(b1.add(b2).doubleValue());
            BigDecimal b3 = new BigDecimal(Double.toString(sum.getActual()));
            BigDecimal b4 = new BigDecimal(Double.toString(itemCapitalExpend.getActual()));
            sum.setActual(b3.add(b4).doubleValue());
        }
        list.add(sum);
        return PlatformResult.success(list);
    }

}
